<template>
  <div class="login">
    <el-form :model="userinfo" :rules="rules" label-width="100px" class="loginInfo" ref="loginInfo">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="userinfo.username" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userinfo.password" clearable  show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {userLogin} from '../request/api.js'
import {mapActions} from 'vuex'
export default {
  data() {
    return {
      userinfo: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    ...mapActions(['changeUser']),
    login() {    
      this.$refs.loginInfo.validate((val) => {
          if (val) {
              // console.log(this.$refs.loginInfo);          
            userLogin(this.userinfo)
            .then(res=>{
              console.log(res)
              if(res.code==200){
                console.log(res,'登录响应')
                this.$message.success(res.msg)
                //将登录结果存到vuex中
                this.changeUser(res.list)
                console.log(res.list,333333)
                //跳转
                 this.$router.push('/index')
              }else{
                this.$message.error(res.msg)
              }
            })
          } else {
            console.log('验证失败');
          }
        });
    }
  }
};
</script>

<style  scoped>
.login {
  width: 100%;
  height: 100vh;
  background: #ccc;
}
.loginInfo {
  width: 400px;
  height: 300px;
  /* background: #fff; */
  position: absolute;
  left: 50%;
  top: 50%;
  margin: -150px 0 0 -200px;
}
</style>
